home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Graphics 2D / Marquee / marquee.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.5 KB  |  173 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        marquee.c
  3.  
  4.     Contains:    This sample will demonstrate an implementation of an "animated" selection box.
  5.  
  6.     Written by: JB    
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include "CarbonPrefix.h"
  26. #include <QuickDraw.h>
  27. #include <Events.h>
  28. #include <Fonts.h>
  29. #include <Menus.h>
  30. #include <Windows.h>
  31. #include <Packages.h>
  32. #include <TextEdit.h>
  33. #include <Dialogs.h>
  34.  
  35. void Resume();
  36. void TrackMarquee(Point start,Rect* resultRect);
  37. void SetMobiusRect(Rect*    rect,short left,short top, short right,short bottom);
  38.  
  39. void Resume() {
  40.     ExitToShell();
  41. }
  42.     
  43. void main() {
  44.     EventRecord        event;
  45.     //WindowRecord    window;
  46.     WindowPtr        window;
  47.     Rect            bounds, rect;
  48.     Point            start;
  49.     
  50.     /*
  51.     ** initialize the macintosh
  52.     */
  53.     /*InitGraf((Ptr) &qd.thePort);
  54.     InitFonts();
  55.     InitWindows();
  56.     InitMenus();
  57.     TEInit();
  58.     InitDialogs(nil);*/
  59.     InitCursor();
  60.         
  61.     SetRect(&bounds, 50, 50, 500, 300);
  62.     //NewWindow((Ptr)&window, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr)-1L, false, 0L);
  63.     //SetPort((GrafPtr)&window);
  64.     window = NewCWindow(nil, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr) -1, false, 0L);
  65.     SetPortWindowPort(window);
  66.     
  67.     
  68.     SetRect(&rect, 0, 0, 0, 0);
  69.     while (true) {
  70.         GetNextEvent(everyEvent, &event);
  71.         switch (event.what) {
  72.             case mouseDown :
  73.                 start = event.where;
  74.                 GlobalToLocal(&start);
  75.                 TrackMarquee(start, &rect);
  76.                 break;
  77.             case keyDown :
  78.                 ExitToShell();
  79.                 break;
  80.         }
  81.     }
  82. }
  83.  
  84. /*
  85. ** Description
  86. **        TrackMarquee will display a marquee similar to the 
  87. **        selection rectangle tool in MacPaint™. It is assumed that the
  88. **        current port has been set before calling. 
  89. **
  90. ** Parameters
  91. **        start        : the local coordinates where the mouse down occured.
  92. **        resultRect    : the final rectangle that was selected.
  93. */
  94.  
  95. #define    TICKDELAY    2
  96.  
  97. void TrackMarquee(Point start,Rect* resultRect)
  98. {    
  99.     /*
  100.     ** there are fifteen patterns defined here 
  101.     ** each one eight bytes long starting at :
  102.     ** patterns[0], patterns[1], patterns[2], patterns[3],
  103.     ** patterns[4], patterns[5], patterns[6], patterns[7]
  104.     */
  105.     static    unsigned char    patterns[] = {
  106.         0xF8, 0xF1, 0xE3, 0xC7, 0x8F, 
  107.         0x1F, 0x3E, 0x7C, 0xF8, 0xF1, 
  108.         0xE3, 0xC7, 0x8F, 0x1F, 0x3E
  109.     };
  110.     
  111.     auto        Point        mouse;        /* the current mouse location */
  112.     register    short        index;        /* the index of the current patterns array */
  113.     auto        Rect        nowRect,    /* the current rectangle to be framed */
  114.                             thenRect;    /* the last rectangle to be framed */
  115.     auto        long        nowTicks,    /* the current tick count */
  116.                             thenTicks;    /* the last tick count */
  117.     auto        PenState    penState;    /* the saved pen state on entry to procedure */
  118.     GrafPtr                    port;
  119.     RgnHandle                rgnHandle = NewRgn();
  120.     
  121.     GetPort(&port);
  122.  
  123.     
  124.     thenTicks = 0;
  125.     index = 0;
  126.     
  127.     GetPenState(&penState);
  128.     PenMode(patXor);
  129.     
  130.     PenPat((Pattern*)&patterns[index]);
  131.     SetRect(&nowRect, start.h, start.v, start.h, start.v);
  132.     FrameRect(&nowRect);
  133.     thenRect = nowRect;
  134.     
  135.     while (StillDown()) {
  136.         nowTicks = TickCount();
  137.         GetMouse(&mouse);
  138.         SetMobiusRect(&nowRect, start.h, start.v, mouse.h, mouse.v);
  139.         if (((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false) || 
  140.         (!EqualRect(&nowRect, &thenRect))) {
  141.             FrameRect(&thenRect);
  142.             index = index < 7 ? index + 1 : 0;
  143.             PenPat((Pattern*)&patterns[index]);
  144.             FrameRect(&nowRect);
  145.             thenRect = nowRect;
  146.         }
  147.         QDFlushPortBuffer(port, GetPortVisibleRegion(port, rgnHandle));
  148.     }
  149.     FrameRect(&thenRect);
  150.     
  151.     SetPenState(&penState);
  152.     *resultRect = thenRect;
  153.     DisposeRgn(rgnHandle);
  154. }
  155.  
  156. void SetMobiusRect(Rect*    rect,short left,short top, short right,short bottom)
  157. {
  158.     if (left > right) {
  159.         rect->left = right;
  160.         rect->right = left;
  161.     } else {
  162.         rect->left = left;
  163.         rect->right = right;
  164.     }
  165.     if (top > bottom) {
  166.         rect->top = bottom;
  167.         rect->bottom = top;
  168.     } else {
  169.         rect->top = top;
  170.         rect->bottom = bottom;
  171.     }
  172. }
  173.